# 71. 手机App防沉迷系统
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
class App {
constructor(name, priority, startTime, endTime) {
this.name = name;
this.priority = priority;
this.startTime = startTime;
this.endTime = endTime;
}
}
const lines = [];
rl.on('line', function(line) {
lines.push(line);
});
rl.on('close', () => {
processInput(lines);
})
function processInput(lines) {
const n = parseInt(lines.shift());
const apps = [];
for(let i=0; i<n; i++) {
const [appName, appPriority, appStartTime, appEndTime] = lines.shift().split(' ');
apps.push(new App(appName, parseInt(appPriority), converTime(appStartTime), converTime(appEndTime)));
}
const queryTime = convertTime(lines.shift());
let appAtTime = "NA";
const registeredApps = [];
for(const app of apps) {
if (app.startTime >= app.endTime) continue;
for(let i=registeredApps.length-1; i>=0; i--) {
const registered = registeredApps[i];
// 存在时间冲突
if (Math.max(app.startTime, registered.startTime) < Math.min(app.endTime, registered.endTime)) {
if (app.priority > registered.priority) {
registeredApps.splice(i, 1);
} else {
continue
}
}
}
registeredApps.push(app);
}
for(const app of registeredApps) {
if (queryTime >= app.startTime && queryTime < app.endTime) {
appAtTime = app.name;
break;
}
}
console.log(appATime);
}
function convertTime(time) {
const [hours, minutes] = time.split(':').map(Number);
return hours*60 + minutes;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66